package com.wcar.user.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.*;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.user.dao.MenuDao;
import com.wcar.user.dao.RoleDao;
import com.wcar.user.dao.RoleMenuDao;
import com.wcar.user.dao.UserRoleDao;
import com.wcar.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: four-wcar-idea
 * @description:
 * @author: 吴朝阳
 * @create: 2024-05-28 09:45
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleMenuDao roleMenuDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<IPage<Role>> getRole(Integer currentPage, Integer pageSize, Role role) {
        IPage<Role> rolePage = new Page<>(currentPage, pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if(role.getRoleName()!=null && role.getRoleName()!=""){
            wrapper.like("role_name",role.getRoleName());
        }
        IPage<Role> list = roleDao.selectPage(rolePage, wrapper);
        return new Result<>(2000,"获取页面成功",list);
    }

    @Override
    public Result<Role> addRole(String username, Role role) {
        role.setCreateBy(username);
        role.setCreateTime(new Date());
        roleDao.insert(role);
        return new Result<>(200,"添加成功",role);
    }

    @Override
    public Result<Role> updateRole(String username, Role role) {
        role.setUpdateBy(username);
        role.getUpdateBy();
        role.setUpdateTime(new Date());
        role.getUpdateTime();
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",role.getRoleId());
        int update = roleDao.update(role, wrapper);
        if (update>0){
            return new Result<>(200,"修改成功",null);
        }
        return new Result<>(500,"修改失败",null);
    }

    @Override
    public Result<Role> deleteRole(Integer roleId) {
        int i = roleDao.deleteById(roleId);
        if (i>0){
            return new Result<>(200,"删除成功",null);
        }
        return new Result<>(500,"删除失败",null);
    }

    @Override
    public Result<List<Role>> selectRole(Integer userId) {
        List<Role> role = roleDao.selectRoleByUserId(userId);
        return new Result<>(200,"获取角色成功",role);
    }
    @Override
    public Result<Role> updateRoles(Integer userId, List<Integer> role) {
        UpdateWrapper<UserRole> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",userId);
        userRoleDao.delete(wrapper);
        for (Integer roleId : role) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleDao.insert(userRole);
        }
        return new Result<>(200,"更改角色成功",null);
    }
    @Override
    public Result<List<Integer>> selectRoleMenus(Integer roleId) {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<RoleMenu> menus = roleMenuDao.selectList(wrapper);
        ArrayList<Integer> list = new ArrayList<>();
        for (RoleMenu menu : menus) {
            list.add(menu.getMenuId());
        }
        return new Result<>(200,"查询角色拥有的菜单",list);
    }

    @Override
    public Result<Integer> update(Integer roleId, ArrayList<Integer> menuIds) {
        UpdateWrapper<RoleMenu> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_id",roleId);
        roleMenuDao.delete(wrapper);
        for (Integer menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuDao.insert(roleMenu);
        }
        return new Result<>(200,"添加角色权限成功",null);
    }

    private List<Menu> getChildren(Integer menuId,List<Menu> menus){
        List<Menu> children = new ArrayList<>();
        for (Menu m :menus){
            if(m.getParentId().equals(menuId)){
                children.add(m);
            }
        }
        for (Menu c:children){
            c.setChildMenu(getChildren(c.getMenuId(),menus));
        }
        return children;
    }

}
